package com.example.accounting.slice;

import com.example.accounting.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;

public class ImportBillSlice extends AbilitySlice {
    private static final int imgRequestCode = 1123;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_import_bill);

        initComponent();
        initImage();
    }

    private void initImage() {
        Image image_wx = (Image) findComponentById(ResourceTable.Id_img_wx);
        Image image_zfb = (Image) findComponentById(ResourceTable.Id_img_zfb);
        setImage(image_wx);
        setImage(image_zfb);
    }

    private void initComponent() {
        DirectionalLayout wx = (DirectionalLayout) findComponentById(ResourceTable.Id_wx);
        DirectionalLayout zfb = (DirectionalLayout) findComponentById(ResourceTable.Id_zfb);

        wx.setClickedListener(component -> {
            System.out.println("click wx");
            Intent intentFile = new Intent();

            // 调用android.intent.action.GET_CONTENT接口访问系统相册
            Operation opt = new Intent.OperationBuilder().withAction("android.intent.action.GET_CONTENT").build();

            intentFile.setOperation(opt);
            intentFile.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
            intentFile.setType("*/*");

            startAbilityForResult(intentFile, imgRequestCode);
        });

        zfb.setClickedListener(component -> {
            System.out.println("click zfb");
            Intent intentFile = new Intent();

            // 调用android.intent.action.GET_CONTENT接口访问系统文件
            Operation opt = new Intent.OperationBuilder().withAction("android.intent.action.GET_CONTENT").build();

            intentFile.setOperation(opt);
            intentFile.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
            intentFile.setType("*/*");

            startAbilityForResult(intentFile, imgRequestCode);
        });
    }

    private void setImage(Image image) {
        PixelMap pixelMap = image.getPixelMap();
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.pixelFormat = pixelMap.getImageInfo().pixelFormat;
        options.editable = true;
        options.size = new Size(250, 250);

        PixelMap pixelMapNew = PixelMap.create(pixelMap, options);
        image.setPixelMap(pixelMapNew);
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        if (requestCode == imgRequestCode) {
            present(new MainAbilitySlice(), new Intent());
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
